﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CommonMethodServiceRef;
    using WMS6.Web.UserControls;

    public class CustomizedPageList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15052;
        protected GridView gvGridviewList;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblCustomizedPageList;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2GridViewList(InitGridviewQueryEntity initGridviewQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(initGridviewQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = CommoneMethodServiceClientProxy.GetInstance().GetInitGridViewInfoByQueryEntity(initGridviewQueryEntity);
            base.BindData2GridView(this.gvGridviewList, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InitGridviewQueryEntity initGridviewQueryEntity = this.GetInitGridviewQueryEntity();
            initGridviewQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = initGridviewQueryEntity.CurrentPage;
            initGridviewQueryEntity.CurrentPage = "0";
            this.gvGridviewList.AllowSorting = false;
            this.BindData2GridViewList(initGridviewQueryEntity);
            this.gvGridviewList.ShowFooter = false;
            this.hdnActionID.Value = "";
            initGridviewQueryEntity.IsGetAll = false;
            initGridviewQueryEntity.CurrentPage = currentPageIndex;
            this.SetInitGridviewQueryEntity(initGridviewQueryEntity);
            base.Export2Excel("InitGridview.xls", this.gvGridviewList);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InitGridviewQueryEntity initGridviewQueryEntity = this.GetInitGridviewQueryEntity();
            initGridviewQueryEntity.CurrentPage = "0";
            this.BindData2GridViewList(initGridviewQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InitGridviewQueryEntity initGridviewQueryEntity = this.GetInitGridviewQueryEntity();
            initGridviewQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2GridViewList(initGridviewQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InitGridviewQueryEntity initGridviewQueryEntity = this.GetInitGridviewQueryEntity();
            initGridviewQueryEntity.FunctionID = "";
            initGridviewQueryEntity.GridviewID = "";
            this.BindData2GridViewList(initGridviewQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InitGridviewQueryEntity initGridviewQueryEntity = this.GetInitGridviewQueryEntity();
            if (int.Parse(initGridviewQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                initGridviewQueryEntity.CurrentPage = (int.Parse(initGridviewQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2GridViewList(initGridviewQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InitGridviewQueryEntity initGridviewQueryEntity = this.GetInitGridviewQueryEntity();
            if (int.Parse(initGridviewQueryEntity.CurrentPage) > 0)
            {
                initGridviewQueryEntity.CurrentPage = (int.Parse(initGridviewQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2GridViewList(initGridviewQueryEntity);
            }
        }

        private InitGridviewQueryEntity GetInitGridviewQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as InitGridviewQueryEntity);
            }
            return (base.PageQueryEntitySession as InitGridviewQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InitGridviewQueryEntity initGridviewQueryEntity = this.GetInitGridviewQueryEntity();
                initGridviewQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2GridViewList(initGridviewQueryEntity);
            }
        }

        protected void gvGridviewList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvGridviewList_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InitGridviewQueryEntity initGridviewQueryEntity = this.GetInitGridviewQueryEntity();
                initGridviewQueryEntity.SortField = e.SortExpression;
                if (initGridviewQueryEntity.SortDirection == "ASC")
                {
                    initGridviewQueryEntity.SortDirection = "DESC";
                }
                else if (initGridviewQueryEntity.SortDirection == "DESC")
                {
                    initGridviewQueryEntity.SortDirection = "ASC";
                }
                this.BindData2GridViewList(initGridviewQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            InitGridviewQueryEntity initGridviewQueryEntity = new InitGridviewQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    initGridviewQueryEntity.FunctionID = this.PreviousPage.FunctionID;
                    initGridviewQueryEntity.GridviewID = this.PreviousPage.GridviewID;
                    initGridviewQueryEntity.FunctionName = this.PreviousPage.FunctionName;
                    initGridviewQueryEntity.FunctionGroupName = this.PreviousPage.FunctionGroupName;
                }
                else
                {
                    initGridviewQueryEntity.FunctionID = "";
                    initGridviewQueryEntity.GridviewID = "";
                    initGridviewQueryEntity.FunctionName = "";
                    initGridviewQueryEntity.FunctionGroupName = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            initGridviewQueryEntity.IsGetAll = false;
            initGridviewQueryEntity.CurrentPage = "0";
            initGridviewQueryEntity.PageSize = this.gvGridviewList.PageSize.ToString();
            initGridviewQueryEntity.SortField = "FUNCTION_ID";
            initGridviewQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvGridviewList.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetInitGridviewQueryEntity(initGridviewQueryEntity);
            this.BindData2GridViewList(initGridviewQueryEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetInitGridviewQueryEntity(InitGridviewQueryEntity initGridviewQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = initGridviewQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = initGridviewQueryEntity;
            }
        }

        public CustomizedPageQuery PreviousPage
        {
            get
            {
                return (CustomizedPageQuery) base.PreviousPage;
            }
        }
    }
}

